package com.zj.rocketmq.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.zj.rocketmq.demo.entity.TOrder;
import com.zj.rocketmq.demo.service.RedisService;
import com.zj.rocketmq.demo.service.TorderService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.UUID;

@RestController
@RequestMapping("order")
public class TestController {
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @Autowired
    RedisService redisService;
    @Autowired
    TorderService torderService;

    @PostMapping("scramble")
    public Object scramble(TOrder tOrder) {
        tOrder.setOrderNo(UUID.randomUUID().toString());
        if (redisService.activityGoingOn()) {
            rocketMQTemplate.convertAndSend("scramble-topic", JSONObject.toJSONString(tOrder));
        }else{
            return Collections.singletonMap("msg","秒杀活动结束!");
        }
        return Collections.singletonMap("msg","秒杀成功，您的订单号为:"+tOrder.getOrderNo());
    }

    @GetMapping("get")
    public TOrder aa(String  orderNo){
        return torderService.getByOrderNo(orderNo);
    }
}
